// index.js
// 获取应用实例
const app = getApp()
import orderApi from '../../api/order'
const FormData = require('../../utils/formData/formData')

Page({
  data: {
    imgFileList:[],
    saleType:null,
    remark:"",
    orderId:""

  },
  onLoad(data) {
    this.setData({
      orderId:data.id
    })
  },

  confirmBtn() {
    if(!this.data.saleType) {
      wx.showToast({
        title: '选择您需要的服务',
        icon: "none",
        duration: 800
      })
      return
    }
    if(!this.data.remark) {
      wx.showToast({
        title: '请描述退换货理由',
        icon: "none",
        duration: 800
      })
      return
    }else {
      if(this.data.remark.length < 10) {
        wx.showToast({
          title: '退换货理由不能少于10个字',
          icon: "none",
          duration: 800
        })
        return
      }
    }

    if(this.data.imgFileList.length == 0) {
      wx.showToast({
        title: '请拍照上传',
        icon: "none",
        duration: 800
      })
      return
    }
    let formData = new FormData()
    let file = this.data.imgFileList[0]
    formData.appendFile("files",file.url);
    formData.append("reason",this.data.remark)
    formData.append("orderId",this.data.orderId)
    formData.append("type", this.data.saleType)
    console.log("formData----->",formData)
    let data = formData.getData();
    orderApi.orderAfterSales(data).then(()=>{
      wx.showToast({
        title: '提交成功!',
        icon: "none",
        duration: 800
      })
     wx.navigateBack()
    })
    
  },
  //删除
  imgDelete(event) {
    let index =  event.detail.index
    let list = this.data.imgFileList
    list.slice(index,1)
    this.setData({ imgFileList:list});
  },
  afterImgRead(event) {
    const { file } = event.detail;
    let list= [...this.data.imgFileList,file]
    this.setData({
      imgFileList:list
    })
    // let formData = new FormData()
    // formData.appendFile("file", file.url);
    // let data = formData.getData();
    // registerApi.businessLicense(data).then((res)=>{
    
    // }).catch((err)=>{
    //   console.log("err------xx----1-->",err)
    // })
  },
  bindTextAreaBlur(e) {
    let  title = e.detail.value
    this.setData({
      remark:title
    })
  }


})
